package manager

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/manager"
	managerReq "gitee.com/g-qs/gin-vue-admin/server/model/manager/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/system"
	"gorm.io/gorm"
)

type FdHealthManagerRegisterService struct {
}

// CreateFdHealthManagerRegister 创建健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) CreateFdHealthManagerRegister(fdHealthManagerRegister *manager.FdHealthManagerRegister) (err error) {
	err = global.GVA_DB.Create(fdHealthManagerRegister).Error
	return err
}

// DeleteFdHealthManagerRegister 删除健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) DeleteFdHealthManagerRegister(fdHealthManagerRegister manager.FdHealthManagerRegister) (err error) {
	err = global.GVA_DB.Delete(&fdHealthManagerRegister).Error
	return err
}

// DeleteFdHealthManagerRegisterByIds 批量删除健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) DeleteFdHealthManagerRegisterByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]manager.FdHealthManagerRegister{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFdHealthManagerRegister 更新健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) UpdateFdHealthManagerRegister(fdHealthManagerRegister manager.FdHealthManagerRegister) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if fdHealthManagerRegister.RegisterStatus != nil && *fdHealthManagerRegister.RegisterStatus == 1 {
			fdHealthManagerRegister.RegisterInfo = "通过"
			err = global.GVA_DB.First(&manager.FdHealthManager{}, "hm_id = ?", fdHealthManagerRegister.HmId).Update("is_register", true).Error
			if err != nil {
				return err
			}
		}
		err = global.GVA_DB.Save(&fdHealthManagerRegister).Error
		return err
	})
}

// GetFdHealthManagerRegister 根据id获取健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) GetFdHealthManagerRegister(id uint) (fdHealthManagerRegister manager.FdHealthManagerRegister, err error) {
	err = global.GVA_DB.Preload("FdHealthManager").Where("id = ?", id).First(&fdHealthManagerRegister).Error
	return
}

// GetFdHealthManagerRegisterInfoList 分页获取健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) GetFdHealthManagerRegisterInfoList(info managerReq.FdHealthManagerRegisterSearch) (list []manager.FdHealthManagerRegister, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Where("store_id = ?", info.FdHealthManager.StoreId).Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Omit("Password")
	}).Model(&manager.FdHealthManagerRegister{})
	var fdHealthManagerRegisters []manager.FdHealthManagerRegister
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerRegisters).Error
	return fdHealthManagerRegisters, total, err
}

// GetScHealthManagerRegisterList 获取健康师注册列表【服务中心】
// Author [g-qs](https://github.com/g-qs)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) GetScHealthManagerRegisterList(info managerReq.FdHealthManagerRegisterSearch, ScID uint) (list []manager.FdHealthManagerRegister, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	var serviceStations []system.SysUser
	err = global.GVA_DB.Model(&system.SysUser{}).Where("creator = ?", ScID).Find(&serviceStations).Error
	var ids []uint
	for _, v := range serviceStations {
		ids = append(ids, v.ID)
	}
	db := global.GVA_DB.Where("store_id IN (?)", ids).Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Omit("Password")
	}).Model(&manager.FdHealthManagerRegister{})
	var fdHealthManagerRegisters []manager.FdHealthManagerRegister
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerRegisters).Error
	return fdHealthManagerRegisters, total, err
}

// GetAllHealthManagerRegisterInfoList 分页获取全部健康师注册记录【超管】
// Author [g-qs](https://gitee.com/g-qs)
func (fdHealthManagerRegisterService *FdHealthManagerRegisterService) GetAllHealthManagerRegisterInfoList(info managerReq.FdHealthManagerRegisterSearch) (list []manager.FdHealthManagerRegister, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Preload("HealthManageCenter").Omit("Password")
	}).Model(&manager.FdHealthManagerRegister{})
	var fdHealthManagerRegisters []manager.FdHealthManagerRegister
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("FdHealthManager.created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerRegisters).Error
	return fdHealthManagerRegisters, total, err
}
